<template>
	<view class="give_silver">
		<Navbar>转赠</Navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="content">
				<view class="title">
					<view>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
						<text>转赠</text>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
					</view>
				</view>
				<view class="cell">
					<view class="key">个人ID</view>
					<view class="value">{{user_id}}</view>
				</view>
				<view class="item">
					<view class="key">数量</view>
					<input type="text" placeholder="请输入要转赠的数量" v-model="number" @input="change">
				</view>
				<view class="item">
					<view class="key">用户ID</view>
					<input type="text" placeholder="请输入要转赠的用户ID" v-model="phone">
				</view>
				<view class="item">
					<view class="key">类型</view>
					<u-radio-group v-model="value">
						<u-radio v-for="(item, index) in radiolist" :label="item.name" :name="item.value">
						</u-radio>
					</u-radio-group>
				</view>
				<view class="cost">
					<view>支付转赠手续费{{payment_channel}}%：{{payment_price}}</view>
					<view>对方预计可获得：{{result}}</view>
				</view>
				<view class="item">
					<view class="key">请输入交易密码</view>
					<input password type="text" placeholder="请输入交易密码" v-model="password">
				</view>
				<view class="btn">
					<view @click="submit">确认转赠</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		userInfo,
		userSilverTurn
	} from '@/request/api.js';

	import {
		field as list,
		// captchaAppId
	} from '@/utils/config.js';

	export default {
		data() {
			return {
				user_id: "",
				payment_channel: 0,
				payment_price: 0,
				result: 0,

				number: '',
				phone: '',
				password: '',

				radiolist: [],
				value: ""
			}
		},
		async onLoad() {
			let radiolist = [];
			for (let key in list) {
				radiolist.push({
					name: list[key],
					value: key
				})
			};
			this.radiolist = radiolist;
			this.value = radiolist.length ? radiolist[0].value : "";

			let {
				silver_turn_rato,
				user
			} = await userInfo();
			this.payment_channel = silver_turn_rato;
			this.user_id = user.id;
		},
		methods: {
			change(e) {
				let price = e.target.value.replace(/[^\d.]/g, '').replace(/(\d)(\.)(\d*)(\2*)(\d*)/, '$1$2$3$5').replace(
					/^(\-)*(\d+)\.(\d{1,4}).*$/, '$1$2.$3');
				this.$nextTick(() => {
					this.price = price;
				});
				let payment_price = this.payment_price = (price * Number(this.payment_channel) / 100).toFixed(4);
				this.result = price - payment_price;
			},
			async submit() {
				if (this.is_click) {
					this.$u.toast("请等待服务器结果");
					return false;
				}
				this.is_click = true;
				await userSilverTurn({
					number: this.number,
					phone: this.phone,
					field: this.value,
					pay_password: this.password
				}, {
					custom: {
						catch: true
					}
				}).then(res => {
					this.is_click = false;
				}).catch(() => {
					this.is_click = false;
				});
				setTimeout(() => {
					this.redirectRouter(1);
				}, 1000);
			}
		}
	}
</script>

<style scoped lang="scss">
	/deep/ .u-radio-group {
		flex-wrap: wrap;

		.u-radio {
			width: 33.33333%;
			margin-bottom: 20rpx;
		}
	}

	.give_silver {
		height: 100vh;
		background: #29170D;
		overflow: hidden;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight});
			padding: 30rpx 25rpx;
			box-sizing: border-box;

			.content {
				width: 688rpx;
				height: 1140rpx;
				padding: 40rpx 55rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/688_1321.png) no-repeat;
				background-size: 100% 100%;
				// width: 100%;
				// height: 800rpx;
				// padding: 40rpx 60rpx;
				// box-sizing: border-box;
				// background: url(#{$baseImageUrl}/bg/750_800.png) no-repeat;
				// background-size: 100% 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.title {
					margin-bottom: 30rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					view {
						height: 30rpx;
						display: flex;
						align-items: center;

						image {
							width: 30rpx;
							height: 30rpx;
						}

						text {
							color: #494338;
							font-size: 42rpx;
							font-weight: 400;
							font-family: LiSu;
							margin: 0 10rpx;
						}
					}
				}

				.cell {
					height: 80rpx;
					margin-bottom: 30rpx;
					display: flex;
					justify-content: space-between;

					.key {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin-bottom: 20rpx;
					}

					.value {
						color: #f00;
						font-weight: 700;
					}
				}

				.item {
					width: 100%;
					border-radius: 10rpx;
					margin-bottom: 30rpx;

					.key {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin-bottom: 20rpx;
					}

					input {
						flex: 1;
						height: 80rpx;
						padding-left: 34rpx;
						box-sizing: border-box;
						box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
						background: #C0B5B6;
					}
				}

				.cost {
					padding: 20rpx 0;
					box-sizing: border-box;

					view {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin: 10rpx 0;
					}
				}


				.btn {
					display: flex;
					justify-content: center;

					view {
						width: 204rpx;
						height: 70rpx;
						line-height: 70rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 30rpx;
						font-weight: 400;
						font-family: SimSun;
						background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
						background-size: 100% 100%;
					}
				}
			}
		}
	}
</style>